home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Arashi 1.1.1 / source code / For your think c folder / Classes / CBaseObject.c next >
Encoding:
C/C++ Source or Header  |  1994-10-14  |  2.5 KB  |  160 lines  |  [TEXT/KAHL]

  1. /*/
  2.      Project Arashi: CBaseObject.c
  3.      Major release: Version 1.1d2, 9/5/95
  4.  
  5.      Last modification: Friday, October 14, 1994, 14:29
  6.      Created: Sunday, July 25, 1993, 7:48
  7.  
  8.      Copyright © 1993-1994, Juri Munkki
  9. /*/
  10.  
  11. #include "CBaseObject.h"
  12.  
  13. #ifdef NO_PRECOMPILED_HEADERS
  14. #include <Memory.h>
  15. #include <OSUtils.h>
  16. #endif
  17.  
  18. #ifdef __cplusplus
  19. static    Size    sObjectSizeTemp;
  20. /*
  21. **    May cause problems with pre-emptive threads.
  22. */
  23. void *    CBaseObject::operator new(
  24.     size_t    reqSize)
  25. {
  26.     sObjectSizeTemp = (Size)reqSize;
  27.     return (void *)new char[reqSize];
  28. }
  29. void     CBaseObject::operator delete(
  30.     void    *thePointer,
  31.     size_t    theSize)
  32. {
  33.     delete [] ((char *)thePointer);
  34. }
  35. CBaseObject::CBaseObject(void)
  36. {
  37.     fObjectSize = sObjectSizeTemp;
  38. }
  39. #endif
  40.  
  41. void    CBaseObject::IBaseObject()
  42. {
  43.     lockCounter = 0;
  44. }
  45.  
  46. void    CBaseObject::Lock()
  47. {
  48.     if(!lockCounter)
  49.         LockThis();
  50.     lockCounter++;
  51. }
  52.  
  53. void    CBaseObject::Unlock()
  54. {
  55.     if(lockCounter != 0)
  56.     {    if(--lockCounter == 0)
  57.         {    UnlockThis();
  58.         }
  59.     }
  60. }
  61.  
  62. void    CBaseObject::LockThis()
  63. {
  64. #ifndef __cplusplus
  65.         HLock((Handle) this);
  66. #endif
  67. }
  68.  
  69. void    CBaseObject::UnlockThis()
  70. {
  71. #ifndef __cplusplus
  72.         HUnlock((Handle) this);
  73. #endif
  74. }
  75.  
  76. void    CBaseObject::ForceUnlock()
  77. {
  78.     if(lockCounter > 1) lockCounter = 1;
  79.     Unlock();
  80. }    
  81.  
  82. void    CBaseObject::Dispose()
  83. {
  84.     delete(this);
  85. }
  86.  
  87. /*
  88. **    This is a utility method for all
  89. **    my classes. The idea is to make
  90. **    copying Handles that are part of
  91. **    an object easy like this:
  92. **
  93. **        thing = CloneHandle(thing);
  94. **
  95. **    Note that you can't simply call
  96. **    HandToHand(&thing), because thing
  97. **    is an instance variable in a block
  98. **    that might move.
  99. */
  100. Handle    CBaseObject::CloneHandle(
  101.     Handle theHandle)
  102. {
  103.     char    state;
  104.     
  105.     state = HGetState(theHandle);
  106.     HandToHand(&theHandle);
  107.     if(theHandle)
  108.         HSetState(theHandle, state);
  109.     
  110.     return theHandle;
  111. }
  112.  
  113. /*
  114. **    Never call this method, but override it,
  115. **    if you are writing a class that should
  116. **    duplicate handles when an object is cloned.
  117. */
  118. void    CBaseObject::CloneFields()
  119. {
  120.  
  121. }
  122.  
  123. /*
  124. **    Call clone to make a deep copy of the object.
  125. */
  126. CBaseObject*    CBaseObject::Clone()
  127. {
  128.     CBaseObject    *myCopy;
  129.     char        state;
  130.     
  131.  
  132. #ifdef __cplusplus
  133.     myCopy = (CBaseObject*) new char[fObjectSize];
  134.     
  135.     if (myCopy) {
  136.         BlockMove((Ptr) this, (Ptr) myCopy, fObjectSize);
  137.         myCopy->CloneFields();
  138.     }
  139. #else
  140.     myCopy = this;
  141.  
  142.     state = HGetState((Handle)this);
  143.     HandToHand((Handle *)&myCopy);
  144.     
  145.     if(myCopy)
  146.     {    HSetState((Handle)myCopy,state);
  147.         myCopy->CloneFields();
  148.     }
  149. #endif    
  150.     return myCopy;
  151. }
  152.  
  153. Size    CBaseObject::HowMuchMemory()
  154. {
  155. #ifdef __cplusplus
  156.     return fObjectSize;
  157. #else
  158.     return GetHandleSize((Handle)this);
  159. #endif
  160. }